/**
 * 
 */
define(function(require) {
	var comm = require('sdk/server');require('sdk/common');
	var userData = comm.getUser();
	
	require('box');

	var dataTemp = $('#msgTemp').val();
	var dataRender = function(data,reload){
		//return appcan.loading.hide();
		var render = etpl.compile(dataTemp);
		var data = data.data;
		if(!data.length){
			appcan.window.disableBounce();
			var emptyhtml = comm.commonTemp('listPlaceholder',{
				icon:'&#xe7ce;',
				text:'暂无消息'
			});
			return $('#View').html(emptyhtml);
		};
		//console.log(data)
		$.each(data,function(i,e){
			if(e.extras && e.extras.split){
				data[i].extras = JSON.parse(e.extras);
			}
		});
		var html = render({
			data: data
		});
		if(reload){
			$('#View').html(html).scrollLoad(getData);
		}else{
			$('#View').append(html).scrollLoad(getData);
		};

		appcan.loading.hide();
	};

	//取消息
	var getData = function(reload){
		appcan.loading.show('正在读取消息');
		appcan.toload({
			url:appcfg.host.control + '/member/listMessage.jsp',
			data:{},
			reload:reload,
			success:function(res){
				appcan.window.resetBounceView(reload ? 0 : 1);
				if(res.status==='Y'){
					dataRender(res,reload);
				}else if(res.status==='N'){
					appcan.loading.hide();
					$.box.msg(res.message,{
						color:'danger',
						delay:2000
					});
				}
			}
		})
	}

	window.change = getData;
	//按钮
	$('#View').on('click','.type_msg',function(){
		var that = this,
			cla = $(that).attr('class'),
			data = $(that).data('id'),
			msgid = $(that).data('msgid');
		//已读
		appcan.ajax({
			url:appcfg.host.control + '/core/service/app/wcm/common/message/app/control.jsp',
			data:{
				method:'readMessage',
				sid:appcfg.project.sid,
				member_id:userData.id,
				msg_id:msgid,
				alias:'user_'+userData.id,
				tag:'user'
			},
			success:function(res){
				//console.log(res)
			}
		});
		if(cla.indexOf('content')>-1){
			//内容消息
			appcan.openView(msgid,'member','msgdetail');
		}else if(cla.indexOf('link')>-1){
			//外链消息
			appcan.openView(null,data);
		}else if(cla.indexOf('news')>-1){
			//新闻消息
			appcan.openView(data,'news','artDetail');
		}else if(cla.indexOf('goods')>-1){
			//商品促销消息
			appcan.openView({
				param:{id:data}
			},'parts','detail');
		}else if(cla.indexOf('serveform')>-1){
			//服务单消息
			alert('服务单消息')
		}else if(cla.indexOf('order')>-1){
			//订单消息
			appcan.openView(data,'shop','orderDetail');
		}else if(cla.indexOf('paper')>-1){
			//问卷消息
			appcan.openView(data,'common','paper');
		}

	});

	appcan.ready(function() {
		appcan.window.setBounce({
			bounceType: ['0'],
			downEndCall: function(type) {
				getData(type==0);
			}
		});

		getData(true);
		

	});
});